package sz.szsmk.citizencard.ui.adapter;

import android.os.Bundle;
import androidx.fragment.app.FragmentActivity;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;

import sz.szsmk.citizencard.DaggerApplication;
import sz.szsmk.citizencard.R;
import sz.szsmk.citizencard.databinding.UserOrderItemBinding;
import sz.szsmk.citizencard.model.network.dzbean.res.MessageEvent;
import sz.szsmk.citizencard.util.ACache;
import sz.szsmk.citizencard.util.Constant;
import sz.szsmk.citizencard.util.JumpUtils;

/**
 * Copyright (c)2018
 * author: zdh
 * created on: 2018/7/23 19:31
 * description:
 */
public class UserOrderItemAdapter extends RecyclerView.Adapter<UserOrderItemAdapter.UserOrderItemHolder> {
    private FragmentActivity mContext;
    private ArrayList<String> mTitle = new ArrayList<>();
    private ArrayList<Integer> iconList = new ArrayList<>();
    private String mExtent;

    public UserOrderItemAdapter(FragmentActivity context, String orderExtent) {
        mContext = context;
        mTitle.add("充值");
        mTitle.add("文化");
        mTitle.add("体育");
        mTitle.add("旅游");
        mTitle.add("商城");
        iconList.add(R.drawable.user_order_cz);
        iconList.add(R.drawable.user_order_wh);
        iconList.add(R.drawable.user_order_ty);
        iconList.add(R.drawable.user_order_ly);
        iconList.add(R.drawable.user_order_sc);
        mExtent = orderExtent;
    }


    @Override
    public int getItemViewType(int position) {
        return R.layout.user_order_item;
    }

    @Override
    public UserOrderItemHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return UserOrderItemHolder.creat(parent);
    }

    @Override
    public void onBindViewHolder(UserOrderItemHolder holder, int position) {
        loadItemData(position, holder.binding);
    }

    private void loadItemData(final int position, UserOrderItemBinding binding) {
        binding.userOrderTv.setText(mTitle.get(position));
        binding.userOrderIcon.setImageResource(iconList.get(position));


        binding.userOrderLl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String access_token = ACache.get(mContext).getAsString("access_token");

                String mextend = ACache.get(DaggerApplication.getIns()).getAsString("mextend");

                if (position == 0) {
                    if (access_token == null) {
                        toLogin();
                    } else {
                        Bundle bundle = new Bundle();
                        bundle.putString("url", Constant.BASE_URL + "app-front-end/#/charge/order?access_token=" + access_token);
                        bundle.putString(Constant.TITLE, "我的订单");
                        JumpUtils.jumptoNatvie(mContext, "webView", 0, bundle);

                    }
                }
                if (position == 1) {
                    if (access_token == null) {
                        toLogin();
                    } else {
                            Bundle bundle = new Bundle();
                            //正式地址
                            bundle.putString("url", Constant.WHXF_URL + "culture/order_mine.html?" + mExtent);
                            JumpUtils.jumptoNatvie(mContext, "webView", 0, bundle);
                    }
                }
                if (position == 2) {
                    if (access_token == null) {
                        toLogin();
                    } else {
                        Bundle bundle = new Bundle();
                        bundle.putString("url", Constant.TYHM_URL + "portal/card_orderlist.html?" + mextend);
                        JumpUtils.jumptoNatvie(mContext, "webView", 0, bundle);

                    }

                }
                if (position == 3) {
                    if (access_token == null) {
                        toLogin();
                    } else {
                        Bundle bundle = new Bundle();
                        bundle.putString("url", Constant.BASE_URL + "app-front-end/#/myOrder/main?access_token=" + access_token);
                        bundle.putString(Constant.TITLE, "asdf");
                        JumpUtils.jumptoNatvie(mContext, "webView", 0, bundle);

                    }
                }
                if (position == 4) {
                    if (access_token == null) {
                        toLogin();
                    } else {
                        Bundle bundle = new Bundle();
                        bundle.putString("url", Constant.BASE_URL + "app-front-end/#/wx/mall?access_token=" + access_token);
                        JumpUtils.jumptoNatvie(mContext, "webView", 0, bundle);

                    }
                }
            }
        });

    }

    public void toLogin() {
        String access_token = ACache.get(mContext).getAsString("access_token");

        if (access_token == null) {
            JumpUtils.jump(mContext, "login", 0);
            return;
        }
    }

    @Override
    public int getItemCount() {
        return 5;
    }

    public static class UserOrderItemHolder extends RecyclerView.ViewHolder {

        private UserOrderItemBinding binding;

        public static UserOrderItemHolder creat(ViewGroup parent) {
            UserOrderItemBinding binding = UserOrderItemBinding
                    .inflate(LayoutInflater.from(parent.getContext()), parent, false);
            return new UserOrderItemHolder(binding);
        }

        public UserOrderItemHolder(UserOrderItemBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }
    }

}
